import { Component, OnInit, Injector } from '@angular/core';
import { CityListComponent } from '../school/cityList';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { HttpClient } from "@angular/common/http";
import { NgZorroPageBaseComponent } from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import {
  PagedResultDto, ClassInfoOutput, PagerSearchInput, HttpService, BaseComponentConfig,
  CustomNavigation, KeyValueOutput, SearchValue
} from '../..';
@Component({
  selector: 'app-authoritymanagement',
  templateUrl: './authoritymanagement.component.html',
  styleUrls: ['./authoritymanagement.component.scss']
})
export class AuthoritymanagementComponent extends NgZorroPageBaseComponent implements OnInit {
  driarr;
  checkOptionsOne;
  autlist;
  radioValue = 'true';
  selectedProvince;
  selectedCity;
  cityData: any;
  schoolName;
  collegelName;
  provinceData: any;
  mess: any;
  resute = [];
  resute1 = [];
  resultxy = [];
  listEntitiesscl;
  timelist = new Array();
  ilist = 0;
  listEntitiesdata: any;
  aclear;
  testval;
  lstCheckedId = [];
  manaId; // 学院id
  aur = []; // 定义boolean
  aclearmana; // 院系定时
  listAcademeConfig = [];
  ngOnInit() {
    // this.IniObserver.subscribe(sb => {
    //   this.getEntityList();
    // });
  }

  constructor(injecter: Injector,
    private modalService: NzModalService,
    public http: HttpService) {
    super(injecter,
      new BaseComponentConfig(
        "DrillProject/GetPageDrillProjectAndSubject",
        "",
        "",
        "",
        "",
        "id",
        "",
        "学校管理",
        [],
        "",
        { Edit: false, Dell: false },
      ));
    const city = new CityListComponent;
    this.cityData = city.cityDatas;
    this.provinceData = city.provinceDatas;

    const datalist = { "Name": "" };
    this.http.post<any>("School/GetSchoolToTen", datalist).then(e => {
      this.listEntitiesdata = e;
    });
  }



  checkSubject(id: number) {
    if (this.lstCheckedId.indexOf(id) < 0) {
      this.lstCheckedId.push(id);
    } else {
      this.lstCheckedId = this.lstCheckedId.filter(e => e !== id);
    }
  }

  /**
   * 保存权限信息
   *
   */
  async serve(type: string) {
    const that = this;
    const postIds = [];
    this.listAcademeConfig.forEach((e) => {
      const subjectIds = e.subjectOutputs.forEach(opt => {
        if (opt.isBuy) {
          postIds.push(opt.id);
        }
      });

    });
    const data = {
      "schoolId": this.manaId, "drillSubjectId": postIds
    };
    await that.http.post<any>("Permissionsa/CreatPermissionsa", data);
    this.message.create(type, `保存成功`);
  }
  async provinceChangesol(e) {
    this.resultxy = await this.http.get<any>('School/GetAllAcademeBySchool?input=' + e);
    this.collegelName = undefined;
  }
  // 获取学院ID
  mabeId(id) {
    this.manaId = id;
    this.getAcademeConfig(id);
  }
  // 学校搜索
  async searchChange(searchText: string) {
    const that = this;
    clearTimeout(this.aclear);
    this.aclear = setTimeout(() => {
      that.sealist(searchText);
    }, 1000);
  }

  sealist(i) {
    const datalist = { "Name": i };
    this.listEntitiesdata = [];
    this.http.post<any>("School/GetSchoolToTen", datalist).then(e => {
      this.listEntitiesdata = e;
    });
  }
  // 院系搜索
  searchChangemana(searchTextmana: string) {
    const that = this;
    clearTimeout(this.aclearmana);
    this.aclearmana = setTimeout(() => {
      that.sealistmana(searchTextmana);
    }, 1000);
  }

  sealistmana(i) {
    // let datalist = { "Name": i };
    // this.listEntitiesdata = [];
    // this.http.post<any>("School/GetSchoolToTen", datalist).then(e => {
    // this.listEntitiesdata = e;
    // });
  }

  cancle() {
    const that = this;
    this.modalService.confirm({
      nzTitle: '<i>确定要全部取消吗?</i>',
      nzOnOk: function () {
        that.collegelName = undefined;
        that.schoolName = undefined;
        that.listAcademeConfig = [];
      }

    });
  }

  async getAcademeConfig(acadeId: number) {
    this.listAcademeConfig = await this.httpService.get<any>(`DrillProject/GetAcademeConfig?acadeid=${acadeId}`);
    this.listAcademeConfig.forEach(e => {
      e['isCheck'] = e.subjectOutputs.filter(sb => sb.isBuy === false).length === 0;
    });
  }

  checkProject(arsubject) {
    arsubject.subjectOutputs.forEach(element => {
      element.isBuy = !arsubject.isCheck;
    });
  }
}

